package thread;

/**
 *
 *两给线程  两把锁----出现死锁现象
 */

public class demo14 {
    private static Object locker1 = new Object();
    private static Object locker2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i <= 5000; i++) {
                try {
                    synchronized (locker1) {
                        Thread.sleep(1000);
                        synchronized (locker2) {
                            System.out.println("t1:" + i);
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i <= 5000; i++) {
                try {
                    synchronized (locker2) {
                        Thread.sleep(1000);
                        synchronized (locker1) {
                            System.out.println("t2:" + i);
                        }
                    }

                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
